(function ($) {
    $.jswzc = {
        comm: {
            contextPath: null,// 系统上下文路径
            // 初始化系统上下文路径
            getContextPath: function () {
                if (!$.jswzc.comm.contextPath) {
                    var jsFileRelativePath = "/jsp/basic/otherBaseDetail/";
                    var scripts = document.getElementsByTagName("script");
                    if (scripts) {
                        for (var i = 0; i < scripts.length; i++) {
                            var src = scripts[i].src;
                            if (src && src.indexOf(jsFileRelativePath) != -1) {
                                var pathArray = src.split(jsFileRelativePath);
                                $.jswzc.comm.contextPath = pathArray[0];
                                break;
                            }
                        }
                    } else {
                        alert("JavaScript初始化异常，请开启浏览器的JavaScript脚本支持！");
                    }
                }

                return $.jswzc.comm.contextPath;
            },
            //加载代理商列表
            loadAgentList:function(type){
                $.ajax({
                    url:$.jswzc.comm.getContextPath()+'/agent/getAgentListForSelect.ac',// 跳转到 action
                    data:{},
                    type:'post',
                    cache:false,
                    dataType:'json',
                    success:function(data) {
                        var html='<option value=""></option>';
                        for(var i=0;i<data.length;i++)
                        {
                            html+='<option value="'+data[i].agent_code+'">'+data[i].agent_name+'</option>';
                        }
                        $("#search_agent"+type).html(html);
                        $('#search_agent'+type).chosen({
                            no_results_text : "未找到此选项!",
                            width:"100%"
                        });
                        $("#search_agent"+type).trigger("chosen:updated");
                        $("#search_agent"+type).chosen().change(function(){
                            loadBrandList($("#search_agent"+type).val(),"");
                        });
                    },
                    error : function() {
                        alert("加载代理商列表失败！");
                    }
                });
            },
            //加载代理商列表
            loadBrandList:function(pid,type){
                $.ajax({
                    url:$.jswzc.comm.getContextPath()+'/merchant/getBrandListForSelect.ac',// 跳转到 action
                    data:{pid:pid},
                    type:'post',
                    cache:false,
                    dataType:'json',
                    success:function(data) {
                        var html='<option value=""></option>';
                        for(var i=0;i<data.length;i++)
                        {
                            html+='<option value="'+data[i].brand_code+'">'+data[i].name+'</option>';
                        }

                        $("#search_brand"+type).html(html);
                        $('#search_brand'+type).chosen({
                            no_results_text : "未找到此选项!",
                            width:"100%"
                        });
                        $('#search_brand'+type).trigger("chosen:updated");
                        $("#search_brand").chosen().change(function(){
                            loadShopList($("#search_brand").val(),"");
                        });
                        $("#search_brand2").chosen().change(function(){
                            loadShopList($("#search_brand2").val(),"2");
                        });
                    },
                    error : function() {
                        alert("加载商户列表失败！");
                    }
                });
            },
            //加载代理商列表
            loadShopList:function(pid,type){
                $.ajax({
                    url:$.jswzc.comm.getContextPath()+'/shop/getShopListForSelect.ac',// 跳转到 action
                    data:{pid:pid},
                    type:'post',
                    cache:false,
                    dataType:'json',
                    success:function(data){
                        var html='<option value=""></option>';
                        for(var i=0;i<data.length;i++)
                        {
                            html+='<option value="'+data[i].sid+'">'+data[i].business_name+'</option>';
                        }

                        $("#search_shop"+type).html(html);
                        $('#search_shop'+type).chosen({
                            no_results_text : "未找到此选项!",
                            width:"100%"
                        });
                        $('#search_shop'+type).trigger("chosen:updated");
                    },
                    error : function() {
                        alert("加载店铺列表失败！");
                    }
                });
            }
        }
    }

})(jQuery);
